package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.MunicipaUnitsSingotenEntity;
import utils.texttools.StringUtil;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/12/4.
 */
public class MunicipaUnitsSingotenAdapter extends
        RecyclerView.Adapter<MunicipaUnitsSingotenAdapter.MunViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<MunicipaUnitsSingotenEntity.ResultEntity> list;

    public MunicipaUnitsSingotenAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<MunicipaUnitsSingotenEntity.ResultEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private boolean isListNull() {
        return list != null && list.size() > 0;
    }

    @Override
    public MunViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.singoten_municipa_units_item, parent, false);
        return new MunViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MunViewHolder holder, int position) {
        if (isListNull()) {
            MunicipaUnitsSingotenEntity.ResultEntity entity = list.get(position);
            holder.sinMunUnitsTitle.setText(entity.getRname());
            holder.sinMunUnitsContent.setText("正在直播 : " + entity.getProgramName());
            holder.sinMunUnitsPlayNum.setText("播放数 : "+ StringUtil.longToWan(entity.getRadioPlayCount()));
            VolleySingleton.getInstance().getBitmap(entity.getRadioCoverLarge(), holder.sinMunUnitsImg);
        }
    }

    @Override
    public int getItemCount() {
        return isListNull() ? list.size() : 0;
    }

    public class MunViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView sinMunUnitsImg;
        private TextView sinMunUnitsTitle, sinMunUnitsContent, sinMunUnitsPlayNum;

        public MunViewHolder(View itemView) {
            super(itemView);
            sinMunUnitsImg = (NetworkImageView) itemView.findViewById(R.id.sinMunUnitsImg);
            sinMunUnitsTitle = (TextView) itemView.findViewById(R.id.sinMunUnitsTitle);
            sinMunUnitsContent = (TextView) itemView.findViewById(R.id.sinMunUnitsContent);
            sinMunUnitsPlayNum = (TextView) itemView.findViewById(R.id.sinMunUnitsPlayNum);
        }
    }
}
